package com.zeus.base.smscenter.utils;

import com.zeus.base.smscenter.mapper.SmsCenterLogMapper;
import com.zeus.base.smscenter.model.Log;
import com.zeus.base.privilege.model.TbProductLineMenu;
import com.zeus.base.privilege.model.TbUser;
import com.zeus.base.privilege.service.IProductLineMenuService;
import com.zeus.base.privilege.utils.StringUtil;
import com.zeus.base.privilege.utils.UserAgentUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * 字典工具类
 * @author ThinkGem
 * @version 2014-11-7
 */
public class LogUtils {
	private static Logger logger = LoggerFactory.getLogger(LogUtils.class);
	private static SmsCenterLogMapper smsCenterLogMapper = com.zeus.base.smscenter.utils.SpringContextHolder.getBean(SmsCenterLogMapper.class);
	private static IProductLineMenuService iProductLineMenuService = com.zeus.base.smscenter.utils.SpringContextHolder.getBean(IProductLineMenuService.class);

	/**
	 * 保存系统业务日志(Controller中传Controller.Class,svaeLog中以Class<?> clazz接收,再通过clazz.getAnnotation(MenuNo.class).value()获取Controller注解的菜单编号)
	 * @param request
	 * @param title(标题)
	 * @param keyId(操作记录的ID)
	 * @param billNo(操作记录的编号)
	 * @param menuNo(菜单编号)
	 * @param methodName(方法名)
	 * @param operate(操作)
	 */
	public static void saveLog(HttpServletRequest request,String title,String keyId,String billNo,String menuNo,String methodName,String operate,String params){
		saveLog(request,null,title,keyId,billNo,menuNo,methodName,operate,params);
	}
	public static void saveLog(HttpServletRequest request,TbUser crrrentUser,String title,String keyId,String billNo,String menuNo,String methodName,String operate,String params){
		Log log = new Log();
		log.setTitle(title);
		log.setKeyId(keyId);
		log.setBillNo(billNo);
		log.setLogType(Log.TYPE_ACCESS);
		log.setParams(params);
		log.setMenuNo(menuNo);
		log.setMethod(methodName);
		log.setOperate(operate);
		TbUser user = crrrentUser;
		if (user==null && request!=null && request.getSession()!=null && request.getSession().getAttribute("sessionUser")!=null)
			user = (TbUser)request.getSession().getAttribute("sessionUser");
		if (user!=null ){
			log.setCreateUserId(user.getUserId());
			log.setCreateUserName(user.getLoginName());
		}
		if (user!=null && StringUtil.isNotEmpty(user.getCompanyIds())){
			log.setCompanyId(StringUtil.startsWithAndEndsWith(user.getCompanyIds()));
			log.setCompanyName(user.getCompanyNames());
		}
		new SaveLogThread(log,request).start();
	}

	/**
	 * 保存日志线程
	 */
	public static class SaveLogThread extends Thread{

		private Log log;
		private HttpServletRequest request;
		public SaveLogThread(Log log,HttpServletRequest request){
			super(SaveLogThread.class.getSimpleName());
			this.log = log;
			this.request = request;
		}
		
		@Override
		public void run() {
			log.setCreateTime(new Date());
			if (request!=null){
				log.setRemoteAddr(UserAgentUtils.getRemoteAddr(request));
				log.setUserAgent(request.getHeader("user-agent"));
				log.setRequestUri(request.getRequestURI());
			}
			if (StringUtils.isNotBlank(log.getMenuNo())){
				Map<String, Object> menuHashMap= null;
				TbProductLineMenu menu= iProductLineMenuService.selectByMenuCode(log.getMenuNo());
				if (menu!=null){
					log.setMenuId(menu.getProductLineMenuId());
					log.setMenuName(menu.getMenuName());
				}
			}
			if (log.getLogType().equals(Log.TYPE_EXCEPTION))
				log.setTitle((log.getRequestUri()==null ? "":log.getRequestUri()==null)+log.getTitle()==null ? "":log.getTitle());
			else if(log.getLogType().equals(Log.TYPE_ACCESS) && StringUtils.isBlank(log.getTitle()))
				log.setTitle((log.getMenuName()==null ? log.getMenuNo():log.getMenuName())+(log.getOperate()==null ? "":log.getOperate()));
			//保存日志信息
			smsCenterLogMapper.insert(log);
		}
	}

	/**
	 * 获取菜单名称路径（如：系统设置-机构用户-用户管理-编辑）
	 * 获取权限中文名称
	 */
	public static String getMenuNamePath(String menuNo, String authorityNo){
		return "";
	}

	
}
